<?php
/**
 * Classe para abstração de URLs. Processa também URLs virtuais do FrameWork
 *
 */
class HTML extends Helper
{
	private $_context;
	
	private $msn;
	
	function HTML($context)
	{
		$this->_context = $context;
	}
	public function url($url)
	{
		$mUrl = '';
		for ($i = 0, $numArgs = func_num_args(); $i < $numArgs ; $i++)
		{
			$value = func_get_arg($i);
			$mUrl .=  $value . '/';	
		}        
    	return $this->_context->webroot() . $mUrl;
	}	

	
	function select($fieldName, $options ,$attributes = '') {
		
		if(is_string($options))
			$options = $this->processAttribute($options);			
		
		if(is_string($attributes))
			$attributes = $this->processAttribute($attributes);
			
		$fieldName = $this->processName($fieldName);		
		$value = $this->_context->data[$fieldName['model']][$fieldName['name']];		

		if(!empty($value))
			$selected = $value;
		else
		{
			$selected = $attributes['value'];
			
		}		
		unset($attributes['value']);
		
		$fk = $attributes['fk']?$attributes['fk']:'';
		$field = $attributes['field']?$attributes['field']:'';
		unset($attributes['fk']);
		unset($attributes['field']);
		
		$select = array();			
		$selectstart = "<select name='%s' %s>";
		$selectend ='</select>';	
		$selectoption = "<option value='%s' %s>%s</option>";
		$selectedOption = "selected='selected'";		
	
		$attributesLine = '';		
		foreach ($attributes as $key => $value)
		{
			$attributesLine .= " $key='$value'" ;
		}
		$select[] = sprintf($selectstart, $fieldName['fieldName'], $attributesLine);
		
		//DebugUtil::PrintValue($options,"line: ".__LINE__ );exit;
		if(count($options))
		{
			foreach ($options as $key => $value)
			{
				if(is_array($value))
				{
					$key = $value[$fk?$fk:$this->_context->getConfig('DATABASE_FK')];
					$value = $value[$field?$field:$this->_context->getConfig('DATABASE_FIELD')];
				}
				
				
				if($key == $selected)			
					$select[] = sprintf($selectoption, $key,$selectedOption, $value);		
				else 
					$select[] = sprintf($selectoption, $key,'', $value);
			}
		}
		$select[] = $selectend;
		
		return implode("\n", $select);
	}	
	
	function checkbox($fieldName, $attributes = '') {
		//<input name="test" type="checkbox" value="1"
		if(is_string($attributes))
			$attributes = $this->processAttribute($attributes);			
			
		$fieldName = $this->processName($fieldName);	
	
		$checked = '';
		if(!empty($this->_context->data[$fieldName['model']])) 
		{
			$value = $this->_context->data[$fieldName['model']][$fieldName['name']];			
			if($value == $attributes['value'])
				$checked = 'checked';
		}
		else
		{
			if(!empty($attributes['compare']) && $attributes['compare'] == $attributes['value'])
			{				
				$checked = 'checked';
			}
			else 
			{
				if(!empty($attributes['checked']))
				{
					$checked = ' checked';
				}				
			}
		}
		//<input name="test" type="checkbox" value="1"  />
		
		$tag = array();	
		
		$tagStartLeft = "<label> %s ";
		$tagFimLeft = "</label>";
		
		$tagFimRight = "%s</label>";
		$tagStarRight = "<label>";
		
		if($attributes['label'])
		{
			switch ($attributes['align'])
			{
				case 'left':
				case 2:
						$tag[] = sprintf($tagStartLeft, $attributes['label']);						
						$fim = $tagFimLeft;
				break;
				default:					
					$fim = sprintf($tagFimRight, $attributes['label']);				
					$tag[]= $tagStarRight;
					
				break;
			}
		}
		$tagtart = "<input name='%s' type='checkbox' %s/>";		
		$attributesLine = $checked . '';		
		
		unset($attributes['checked']);
		unset($attributes['compare']);
		unset($attributes['label']);
		unset($attributes['align']);
					
		foreach ($attributes as $key => $value)
		{
			$attributesLine .= " $key='$value'" ;
		}		
		$tag[] = sprintf($tagtart, $fieldName['fieldName'],$attributesLine);
		$tag[] = $fim;
		return implode("\n", $tag);
	}	
	
	public function Show()
	{
		$msn = $this->_context->getSession('myShowF');		
		return $msn;		
	}
	
	public function Real($number)
	{
		return number_format($number, 2, ",", ".");
	}
}
?>